package ar.com.cooker.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import ar.com.cooker.dao.RecipeIngredientDao;
import ar.com.cooker.model.Ingredient;
import ar.com.cooker.model.Recipe;
import ar.com.cooker.model.RecipeIngredient;
import ar.com.cooker.model.RecipeIngredientId;

@Repository
@Transactional
public class RecipeIngredientDaoImpl implements RecipeIngredientDao{

	private EntityManager entityManager;
	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RecipeIngredient> findAll() {
		Query q = getEntityManager().createNamedQuery("Select * FROM ck_ingredient_recipe");
		return q.getResultList();
	}

	@Override
	public RecipeIngredient findById(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void save(RecipeIngredient t) {
		RecipeIngredientId id = new RecipeIngredientId();
		if((t.getIngredient() != null && t.getIngredient().getId() != null)&&(t.getRecipe() != null && t.getRecipe().getId() != null)){
			Ingredient i = getEntityManager().find(Ingredient.class, t.getIngredient().getId());
			Recipe p = getEntityManager().find(Recipe.class, t.getRecipe().getId());
			id.setIngredient(i);
			id.setRecipe(p);
			t.setId(id);
			getEntityManager().persist(t);
		}else{
			//TODO excepcion
		}
	}

	@Override
	public void update(RecipeIngredient t) {
		RecipeIngredientId id = new RecipeIngredientId();
		if((t.getIngredient() != null && t.getIngredient().getId() != null)&&(t.getRecipe() != null && t.getRecipe().getId() != null)){
			Ingredient i = getEntityManager().find(Ingredient.class, t.getIngredient().getId());
			Recipe p = getEntityManager().find(Recipe.class, t.getRecipe().getId());
			id.setIngredient(i);
			id.setRecipe(p);
			t.setId(id);
			getEntityManager().merge(t);
		}else{
			//TODO excepcion
		}
		
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Ingredient> findIngredientsByIdRecipe(Integer idRecipe) {

		Query q = getEntityManager().createQuery(
				"SELECT i FROM Ingredient i,RecipeIngredient ri "
						+ "WHERE ri.id.recipe.id =: id_recipe AND i.id = ri.id.ingredient.id").setParameter(
				"id_recipe", idRecipe);
		return q.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Recipe> findRecipesByIdIntegrient(Integer idIngredient) {
		Query q = getEntityManager().createQuery(
				"SELECT i FROM Recipe r,RecipeIngredient ri "
						+ "WHERE ri.id.ingredient.id =: id_ingredient AND r.id = rc.id.recipe.id")
				.setParameter("id_ingredient", idIngredient);
		return q.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Recipe> findRecipesByDescriptionIngredient(String name) {
		Query q = getEntityManager().createQuery(
				"SELECT i FROM Recipe r ,RecipeIngredient ri,Ingredient i "
						+ "WHERE r.id = rc.id.recipe.id  AND ri.id.ingredient.id = i.id AND i.name like '%name%'")
				.setParameter("name", name);
		return q.getResultList();
	}
	
	
	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
	
	public EntityManager getEntityManager() {
		return entityManager;
	}

}
